package com.fzt.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzt.reggie.common.CustomException;
import com.fzt.reggie.entity.Category;
import com.fzt.reggie.entity.Dish;
import com.fzt.reggie.entity.Setmeal;
import com.fzt.reggie.mapper.CategoryMapper;
import com.fzt.reggie.service.CategoryService;
import com.fzt.reggie.service.DishService;
import com.fzt.reggie.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetMealService setMealService;
    /*
    根据id删除分类
     */
    @Override
    public void remove(Long id) {
        //查询当前分类是否关联了，关联了就抛出异常
        //构造查询条件
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        //判断是否含有菜品
        if (count1 > 0){
            //存在菜品关联,抛出异常
            throw new CustomException("存在菜品关联,无法删除");
        }

        //查询当前分类是否关联了，关联了就抛出异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setMealService.count(setmealLambdaQueryWrapper);
        if (count2 > 0){
            //存在套餐关联，抛出异常
            throw new CustomException("存在套餐关联,无法删除");
        }
        //不存在关联，正常删除
        super.removeById(id);


    }
}
